package com.seagull.sb.service;

import javax.ws.rs.core.SecurityContext;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.seagull.sb.bo.BaseBO;
import com.seagull.sb.security.SBSecurityContext;
import com.seagull.sb.security.UserStudent;
import com.seagull.sb.security.UserTeacher;
import com.seagull.sb.util.SBRequestContext;

public class BaseService implements ApplicationContextAware {
	private ApplicationContext ctx;
	@Override
	public void setApplicationContext(ApplicationContext ctx)
			throws BeansException {
			this.ctx = ctx;
	}
	public BaseBO getBean(String name){
		return (BaseBO)ctx.getBean(name);
	}
	
	public SBRequestContext populateRequestContext(SecurityContext sccontext) {
		SBRequestContext sb = new SBRequestContext();
		
		if(sccontext.getUserPrincipal() instanceof UserStudent){
			UserStudent user = (UserStudent)sccontext.getUserPrincipal();
			sb.setClassId(user.getClassId());
			sb.setOrgId(user.getOrgId());
			sb.setStudentId(user.getStudentId());
			sb.setTeacherId(0);
			sb.setStudent(user.getStudent());
		}else if (sccontext.getUserPrincipal() instanceof UserTeacher){
			UserTeacher user = (UserTeacher)sccontext.getUserPrincipal();
			sb.setClassId(user.getClassId());
			sb.setOrgId(user.getOrgId());
			sb.setStudentId(0);
			sb.setTeacherId(user.getTeacherId());
			sb.setTeacher(user.getTeacher());
		}
		else {
			// log statement 
		}
		return sb;
	}
}
